<?php
$port = 10240;
$ip = "socket.evildao.com";
/*
 +-------------------------------
 *    @socket连接整个过程
 +-------------------------------
 *    @socket_create
 *    @socket_connect
 *    @socket_write
 *    @socket_read
 *    @socket_close
 +--------------------------------
 */

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
    echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
}
echo "Connent $ip:$port...";
$result = socket_connect($socket, $ip, $port);
if ($result < 0) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
}else {
    echo "OK\n";
}
while(1) {
	$str = trim(fgets(STDIN));
	//$str = @iconv('utf-8', 'gbk', $str);
	socket_write($socket, $str, strlen($str));
}
socket_close($socket);